home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / C++ Files Library / Graphic Gems I, II & III (C_C++) / Graphics Gems C Code.sea / GemsII / ran_ramp.c < prev    next >
C/C++ Source or Header  |  1992-06-16  |  7KB  |  308 lines

  1. /* copyright Ken Musgrave */
  2. /* March 1985 */
  3.  
  4. /* ran_ramp.c */
  5.  
  6. /***********************************************************************
  7. *
  8. *   Usage: ran_ramp [-g] [-z]
  9. *
  10. *   Performs random continuous changes to the color map of a frame buffer.
  11. *
  12. *   The idea is to use three DDA's with endpoint input from a random
  13. * number generator.  The three DDA's generate random sawtooth waves
  14. * of values for red, green, and blue.  These waves of values are pushed
  15. * through the lookup table from entry 254 down to entry 0.
  16. *
  17. *   NOTE:  Entry 255 remains black, as this was designed for animating
  18. * the Mandelbrot set, whose center should remain black.
  19. *
  20. ***********************************************************************/
  21.  
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <signal.h>
  25.  
  26. #define TRUE  1
  27. #define FALSE 0
  28.  
  29. #define MAXENTRY    256
  30. #define MAXINDEX    255
  31. #define DELAY        8    /* kludge to modulate speed of animation */
  32.                 /* you may want/need to change this value */
  33.  
  34. /*
  35.  * pseudo-random number generator; period 65536; requires seed between 0 and
  36.  * 65535; returns random numbers between 0 and 65536.
  37.  */
  38. #define MULTIPLIER 25173
  39. #define INCREMENT  13849
  40. #define MODULUS    65535
  41. #define RANDOM(x)  (MULTIPLIER * x + INCREMENT) & MODULUS
  42.  
  43. int             quit = FALSE;    /* signal variables */
  44. int             stop = FALSE;    /* used in trapping <ctrl-C> and <ctrl-Z> */
  45.  
  46. int             seed, maxsteps; /* user input variables */
  47.  
  48.                 /* arrays for color lookup table values */
  49. unsigned char   r[MAXENTRY], g[MAXENTRY], b[MAXENTRY];
  50.  
  51.  
  52. main(argc, argv)
  53. int             argc;
  54. char           *argv[];
  55. {
  56.  
  57.     register int    i;
  58.     int             reply, delay;
  59.  
  60.     int             inter();     /* signal functions, see below */
  61.     int             suspend();
  62.  
  63.     signal(SIGINT, inter);         /* traps <ctrl-C> */
  64.     signal(SIGTSTP, suspend);     /* traps <ctrl-Z> */
  65.  
  66.     printf("Please enter seed: ");
  67.     scanf("%d", &seed);
  68.  
  69.                     /* "(10-100)" is just a suggestion... */
  70.     printf("\nPlease enter maximum length of color ramp:  (10-100)");
  71.     scanf("%d", &maxsteps);
  72.     printf("\n");
  73.  
  74.     fb_init();            /* generic frame buffer init routine */
  75.  
  76.             /* if specified, set initial map option */
  77.     if (argc == 2) {
  78.             /* "-g" option for gray scale */
  79.         if (argv[1][0] == '-' && argv[1][1] == 'g') {
  80.             for (i=0; i<MAXINDEX; i++)
  81.                 r[i] = g[i] = b[i] = i;
  82.             r[MAXINDEX] = g[MAXINDEX] = b[MAXINDEX] = 0;
  83.         }
  84.             /* "-z" option for zebra scale */
  85.         else if (argv[1][0] == '-' && argv[1][1] == 'z') {
  86.             for (i=0; i<MAXINDEX; i+=4) {
  87.                 r[i]=r[i+1]=g[i]=g[i+1]=b[i]=b[i+1]=i * 4 / 5;
  88.                 r[i+2]=r[i+3]=g[i+2]=g[i+3]=b[i+2]=b[i+3]=
  89.                     i * 4/5 + 51;
  90.             }
  91.             r[MAXINDEX] = g[MAXINDEX] = b[MAXINDEX] = 0;
  92.         }
  93.     } else {
  94.             /* initialize the color map to black */
  95.         for (i=0; i<MAXINDEX; i++)
  96.             r[i] = g[i] = b[i] = 0;
  97.     }
  98.  
  99.             /* generic routine to write frame buffer color map */
  100.     fb_setmap(r, g, b);
  101.  
  102.             /* loop until <ctrl-C> is trapped */
  103.     while (!quit) {
  104.             /* trapped <crtl-Z> */
  105.         if (stop) {
  106.             fb_done();    /* release frame buffer */
  107.             printf("\nSave lookup table? (y/n) ");
  108.             while (isspace(reply = getchar()));
  109.             if (reply == 'y')
  110.                 save_lut();
  111.                     /* returns when job running again */
  112.             kill(getpid(), SIGSTOP);
  113.             stop = FALSE;
  114.             fb_init();    /* get back in action */
  115.         }
  116.  
  117.         /*
  118.          * main animation loop:
  119.          *
  120.          * move each entry in color map arrays down one place
  121.          */
  122.         for (i=0; i<(MAXINDEX-1); i++) {
  123.             r[i] = r[i+1];
  124.             g[i] = g[i+1];
  125.             b[i] = b[i+1];
  126.         }
  127.         /* 
  128.          * get new high color map entries 
  129.          */
  130.         r[MAXINDEX-1] = dda_red();
  131.         g[MAXINDEX-1] = dda_green();
  132.         b[MAXINDEX-1] = dda_blue();
  133.         /*
  134.          * send new color maps to the frame buffer
  135.          * NOTE: we may do this several times (the value of
  136.          * "DELAY") to slow down the animation
  137.          */
  138.         for (delay=0; delay<DELAY; delay++)
  139.             fb_setmap(0, MAXINDEX, r, g, b);
  140.  
  141.     }
  142.  
  143.     fb_done();    /* release the frame buffer */
  144.  
  145. } /* main() */
  146.  
  147.  
  148. /*
  149.  * function for trapping <ctrl-C>
  150.  * NOTE: the only reason to use this is so that
  151.  * we can call fb_done() before exiting - this 
  152.  * may or may not be necessary
  153.  */
  154. inter()
  155. {
  156.     quit = TRUE;
  157. }
  158.  
  159. /*
  160.  * function for trapping <ctrl-Z>
  161.  * NOTE: this is used so that the user may stop the
  162.  * animation at any time and dump the color map to 
  163.  * a file
  164.  */
  165. suspend()
  166. {
  167.     stop = TRUE;
  168. }
  169.  
  170.  
  171. /*
  172.  * produces linear ramps in intensity for the red portion of the color
  173.  * lookup table
  174.  */
  175.  
  176. dda_red()
  177. {
  178.  
  179.     register int    temp;
  180.     static float    ry1, ry2=0., rinc, r_xsteps=0.;
  181.     static int     r_xcount=0;
  182.  
  183.     /* if at end of ramp... */
  184.     if (r_xcount >= (int) r_xsteps) {
  185.         /*
  186.          * make the end of last ramp the beginning of next ramp
  187.          */
  188.         ry1 = ry2;
  189.             /* define end of next ramp */
  190.         seed = RANDOM(seed);
  191.             /* assign a new (scaled) end point */
  192.         ry2 = MAXINDEX * (seed / 65535.0);
  193.         seed = RANDOM(seed);
  194.             /* get a new ramp length */
  195.         r_xsteps = (maxsteps * (seed / 65535.0));
  196.             /* find the intensity increment per step */
  197.         if (r_xsteps != 0)
  198.             rinc = (ry2 - ry1) / r_xsteps;
  199.         else
  200.             rinc = 0;
  201.         r_xcount = 0;
  202.     }
  203.     temp = (int) ry1;
  204.     ry1 += rinc;
  205.     r_xcount++;
  206.     return temp;
  207. } /* dda_red() */
  208.  
  209. /*
  210.  * produces linear ramps in intensity for the green portion of the color
  211.  * lookup table
  212.  */
  213.  
  214. dda_green()
  215. {
  216.  
  217.     register int    temp;
  218.     static float    gy1, gy2=0., ginc, g_xsteps=0.;
  219.     static int     g_xcount=0;
  220.  
  221.     if (g_xcount >= (int) g_xsteps) {
  222.         /*
  223.          * make the end of last ramp the beginning of next ramp
  224.          */
  225.         gy1 = gy2;
  226.             /* define end of next ramp */
  227.         seed = RANDOM(seed);
  228.         gy2 = MAXINDEX * (seed / 65535.0);
  229.         seed = RANDOM(seed);
  230.         g_xsteps = (maxsteps * (seed / 65535.0));
  231.             /* find the intensity increment per step */
  232.         if (g_xsteps != 0)
  233.             ginc = (gy2 - gy1) / g_xsteps;
  234.         else
  235.             ginc = 0;
  236.         g_xcount = 0;
  237.     }
  238.     temp = (int) gy1;
  239.     gy1 += ginc;
  240.     g_xcount++;
  241.     return temp;
  242. }  /* dda_green() */
  243.  
  244. /*
  245.  * produces linear ramps in intensity for the blue portion of the color
  246.  * lookup table
  247.  */
  248.  
  249. dda_blue()
  250. {
  251.  
  252.     register int    temp;
  253.     static float    by1, by2=0., binc, b_xsteps=0.;
  254.     static int     b_xcount=0;
  255.  
  256.     if (b_xcount >= (int) b_xsteps) {
  257.         /*
  258.          * make the end of last ramp the beginning of next ramp
  259.          */
  260.         by1 = by2;
  261.             /* define end of next ramp */
  262.         seed = RANDOM(seed);
  263.         by2 = MAXINDEX * (seed / 65535.0);
  264.         seed = RANDOM(seed);
  265.         b_xsteps = (maxsteps * (seed / 65535.0));
  266.             /* find the intensity increment per step */
  267.         if (b_xsteps != 0)
  268.             binc = (by2 - by1) / b_xsteps;
  269.         else
  270.             binc = 0;
  271.         b_xcount = 0;
  272.     }
  273.     temp = (int) by1;
  274.     by1 += binc;
  275.     b_xcount++;
  276.     return temp;
  277. }  /* dda_blue() */
  278.  
  279.  
  280. /* 
  281.  * save the lookup table to a file
  282.  */
  283.  
  284. save_lut()
  285. {
  286.     FILE           *fp, *fopen();
  287.     char            filename[40];
  288.     int             i;
  289.  
  290.     getchar();        /* read leading newline char */
  291.     printf("Enter filename for lookup table:  ");
  292.     gets(filename);
  293.     fp = fopen(filename, "w");
  294.  
  295.     for (i=0; i<MAXINDEX; i++)
  296.         fprintf(fp, "%3d %3d %3d\n", r[i], g[i], b[i]);
  297.  
  298.     fclose(fp);
  299.  
  300. } /* save_lut() */
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.